package com.hou.shop.product.service.impl;

import com.hou.shop.product.entity.AttrGroupEntity;
import com.hou.shop.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.common.utils.PageUtils;
import com.hou.common.utils.Query;

import com.hou.shop.product.dao.CategoryDao;
import com.hou.shop.product.entity.CategoryEntity;
import com.hou.shop.product.service.CategoryService;
import org.springframework.util.StringUtils;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        // 查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);

        // 组装父子树结构
        List<CategoryEntity> level1Menus = entities.stream().filter((categoryEntity -> {
            return categoryEntity.getParentCid() == 0;
        })).map((menu) -> {
            menu.setChildren(getChildrend(menu,entities));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return level1Menus;
    }

    @Override
    // 逻辑删除菜单
    public void logicDelete(List<Long> asList) {
        // TODO 检查当前菜单是否被其他菜单引用
        baseMapper.deleteBatchIds(asList);
    }

    // 查询出指定id的完整路径
    @Override
    public Long[] getPath(Long catelogId) {
        List<Long> path = new ArrayList<>();
        // 递归查询id所属完整目录
        List pathByRecursion = getPathByRecursion(path, catelogId);

        return (Long[]) pathByRecursion.toArray(new Long[pathByRecursion.size()]);
    }

    // 冗余更新
    @Override
    public void updateDetail(CategoryEntity category) {
        Long catId = category.getCatId();
        String categoryName = category.getName();
        this.updateById(category);
        if (!StringUtils.isEmpty(categoryName)) {
            // 冗余更新品牌分类关系表
            categoryBrandRelationService.updateCategoryName(catId,categoryName);

        }
    }

    // 递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildrend(CategoryEntity root, List<CategoryEntity> all) {

        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().equals(root.getCatId());
        }).map(categoryEntity -> {
            // 找到子菜单
            categoryEntity.setChildren(getChildrend(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            // 菜单排序
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

    // 递归查询cateid完整路径
    private List getPathByRecursion (List path, Long cateLogId) {

        CategoryEntity category = this.getById(cateLogId);
        if (category.getParentCid() != 0) {
            getPathByRecursion(path,category.getParentCid());
        }
        // 将自己的catId放入
        path.add(cateLogId);
        return path;
    }

}